package com.woniuxy.controller;


import com.woniuxy.dto.ResultChangeServer;
import com.woniuxy.dto.ResultServerDetail;
import com.woniuxy.exception.ErrorException;
import com.woniuxy.pojo.ServerMenu;
import com.woniuxy.pojo.ServerSubscribe;
import com.woniuxy.pojo.Servers;
import com.woniuxy.service.AlipayService;
import com.woniuxy.service.ServerMenuService;
import com.woniuxy.service.ServerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/server")
@CrossOrigin(allowCredentials="true",maxAge = 3600)
@Api(tags = "便民服务类")
public class ServerController {


    @Autowired
    private ServerService serverService;

    @Autowired
    private ServerMenuService serverMenuService;

    @Autowired
    private AlipayService alipayService;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private HttpServletRequest request;

    //跳转便民服务页
    @GetMapping("/menu")
    @ResponseBody
    @ApiOperation(value = "加载服务页所需的参数",notes = "热门服务和所有服务")
    public Map toServerMenu(){
        Map<String,Object> map = new HashMap<>();
        //查询出所有的菜单及其子菜单
        List<ServerMenu> lists = serverService.findMenu();
        //查询出热门3个热门服务
        List<ServerMenu> hotLists = serverService.findHotServer();
        map.put("lists",lists);
        map.put("hotLists",hotLists);
        return map;
    }

    //跳转选择服务页面后加载数据
    @GetMapping("/changeServer")
    @ResponseBody
    @ApiOperation(value = "选择服务页面所需的参数")
    @ApiImplicitParam(name = "serverMenuId",value = "服务菜单表ID",required = true,dataType = "Long")
    public List<ResultChangeServer> toServerDetail(Long serverMenuId){

        List<ResultChangeServer> lists = serverService.findServersByMenuId(serverMenuId);

        return lists;
    }

    //加载服务详情页信息
    @GetMapping("/detail")
    @ResponseBody
    @ApiOperation(value = "服务详情页面所需的参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mid",value = "物业或商家id",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "typeId",value = "类型，0表示物业，1表示商家",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "id",value = "服务菜单表id",required = true,dataType = "Long")
    })
    public Map toServerDetail(@RequestParam("mid") Long mid,
                                       @RequestParam("typeId") Integer typeId,
                                       @RequestParam("id") Long id){
        ResultServerDetail rsd = null;
        Map<String,Object> map = new HashMap<>();
        //System.out.println(mid+".."+typeId+".."+id);
        try {

            rsd = serverService.findProviderByMidAndTypeId(mid,typeId,id);
            map.put("code",200);
            map.put("rsd",rsd);
            return map;
        } catch (ErrorException e) {
            e.printStackTrace();
            map.put("code",500);
            return map;
        }
    }



    //生成预约信息，插入数据库
    @PostMapping("/createSubscribe")
    @ResponseBody
    @ApiOperation(value = "生成预约信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "serverId",value = "服务表id",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "datetime",value = "预约时间,如(2020-04-07 18:20)",required = true,dataType = "Date")
    })
    public Map createSubscribe(Long userId,Long serverId,String datetime){
        System.out.println(userId+"/"+serverId+"/"+datetime);
        datetime = datetime+":00";
        Map<String,Object> map = new HashMap();
        //讲前端传入的字符串改为date
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            Date date = sdf1.parse(datetime); //拿到对象
            Date nowdate = new Date();
            if(date.before(nowdate)){
                map.put("code",500);
                map.put("msg","预约时间不能小于当前时间！");
                return map;
            }
            ServerSubscribe serverSubscribe = new ServerSubscribe();
            serverSubscribe.setServerId(serverId);
            serverSubscribe.setUserId(userId);
            serverSubscribe.setSubscribeTime(date);
            serverSubscribe.setStatus(1);
            //System.out.println(serverSubscribe+"//////////////////////");
            serverService.insertServerSubscribe(serverSubscribe);
            map.put("code",200);
            map.put("msg","预约成功！");
            return map;
        } catch (ParseException e) {
            e.printStackTrace();
            map.put("code",500);
            map.put("msg","预约失败请选择正确的时间格式！");
            return map;
        } catch (Exception e){
            map.put("code",500);
            map.put("msg","预约失败！");
            return map;
        }


    }
}
